package com.es.dao.factory;

import java.io.Reader;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.es.exception.dao.ESDataAccessException;


public class SqlMapClientFactory {
	
	private static final String SQL_MAP_CONFIG_FILE_PATH = "/sqlMapConfig.xml";
	
	private static SqlMapClient sqlMapClient;
	
	static {
		
		try {
			
			String resource = SqlMapClientFactory.SQL_MAP_CONFIG_FILE_PATH;
			Reader reader = Resources.getResourceAsReader(resource);
			sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new ESDataAccessException("Error initializing IBatisSqlMapClient. Cause: " + e);
		}
		
	}

	
	public static SqlMapClient getSqlMapClient() {
			
			if(sqlMapClient == null){
				throw new ESDataAccessException("Error initializing SqlMapClient due to previous error.");
			}
			
			return sqlMapClient;
			
	}
	
}